package com.gt.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Slf4j
@Component
public class RedisUtil {
    @Autowired
    StringRedisTemplate stringRedisTemplate;

    public boolean isExist(String key,int expire) {
        long count = stringRedisTemplate.opsForValue().increment(key);
        if (count==1){
            stringRedisTemplate.expire(key,expire, TimeUnit.SECONDS);
            return false;   //首次创建
        }
        return true;   //存在
    }

    public void removeLockKey(String key){
        stringRedisTemplate.delete(key);
    }
}
